//
//  ViewController.m
//  02-Lagou
//
//  Created by 韩旭 on 16/7/15.
//  Copyright © 2016年 基础. All rights reserved.
//
#import "ViewController.h"
#import "PassView.h"
#import "UIImageView+TouchImage.h"
#import "UILable_HX.h"
@interface ViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) UIImageView *imageBackground;
@property (nonatomic, strong) UIImageView *imageUser;
@property (nonatomic, strong) UIImageView *imagePass;
@property (nonatomic, strong) UIView *viewBlur;
@property (nonatomic, strong) UILabel *lableLine;
@property (nonatomic, strong) UIImageView *imageLagou;
@property (nonatomic, strong) UIView *userView;
@property (nonatomic, strong) UIView *viewUser;
@property (nonatomic, strong) UIView *viewPass;
@property (nonatomic, strong) UIButton *btnRegister;
@property (nonatomic, strong) UIButton *btnPass;
@property (nonatomic, strong) UIButton *btnSign;
@property (nonatomic, assign) BOOL isPanduan;
@property (nonatomic, strong) UITextField *fieldUser;
@property (nonatomic, strong) UITextField *fieldPass;
@property (nonatomic, strong) UILabel *lable;
@property (nonatomic, strong) UIButton *btnDelegate;
@property (nonatomic, strong) UIButton *lableAgree;
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.imageBackground = [UIImageView packImageViewFrame:CGRectMake(0, 0, WIDETH, HEIGHT) imagePhoto:[UIImage imageNamed:@"welcome_2"] colorBack:nil action:nil target:self];
    [self.view addSubview:_imageBackground];
    self.viewBlur = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDETH, HEIGHT)];
    self.viewBlur.backgroundColor = [UIColor lightGrayColor];
    self.viewBlur.alpha = 0.3;
    [self.view addSubview:_viewBlur];
    [self creatLagouImage];
    [self creatUserPassBack];
    [self creatBtnRegister];
    self.isPanduan = NO;
    [self creatBtn];
}
- (void)creatLagouImage {
    self.imageLagou = [UIImageView packImageViewFrame:CGRectMake(0, 0, WIDETH * 0.245, HEIGHT * 0.225) imagePhoto:[UIImage imageNamed:@"logo_lagou"] colorBack:nil action:nil target:self];
    
    self.imageLagou.center = CGPointMake(self.viewBlur.frame.size.width / 2, HEIGHT * 0.23);
    
    [self.view addSubview:_imageLagou];
}
- (void)creatUserPassBack {
    self.userView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDETH *0.95, HEIGHT * 0.16)];
    self.userView.center = CGPointMake(WIDETH / 2, HEIGHT * 0.525);
    self.userView.layer.cornerRadius = 5;
    self.userView.backgroundColor = [UIColor colorWithRed:0.29 green:0.29 blue:0.27 alpha:1.00];
    self.userView.alpha = 0.7;
    self.lableLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.userView.frame.size.width, 2)];
    self.lableLine.backgroundColor = [UIColor grayColor];
    self.lableLine.center = self.userView.center;
    [self.view addSubview:_userView];
    [self.view addSubview:_lableLine];
    self.fieldUser = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.userView.frame.size.width, self.userView.frame.size.height / 2 - self.lableLine.frame.size.height / 2)];
    self.fieldUser.backgroundColor = [UIColor clearColor];
    //    self.fieldUser.placeholder = @" 请输入已验证手机号或邮箱";
    NSDictionary *dicUser = @{NSForegroundColorAttributeName:[UIColor grayColor], NSFontAttributeName:[UIFont systemFontOfSize:20.0]};
    self.fieldUser.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" 请输入已验证手机号或邮箱" attributes:dicUser];
    self.fieldUser.delegate = self;
    self.fieldUser.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.userView.frame.size.height / 2 - self.lableLine.frame.size.height / 2, self.userView.frame.size.height / 2 - self.lableLine.frame.size.height / 2)];
    self.fieldUser.leftView.backgroundColor = [UIColor colorWithRed:0.12 green:0.09 blue:0.07 alpha:1.00];
    self.fieldUser.leftViewMode = UITextFieldViewModeAlways;
    [self.userView addSubview:_fieldUser];
    self.fieldPass = [[UITextField alloc] initWithFrame:CGRectMake(0, self.fieldUser.frame.size.height + self.fieldUser.frame.origin.y + self.lableLine.frame.size.height, self.fieldUser.frame.size.width, self.fieldUser.frame.size.height)];
    self.fieldPass.backgroundColor = [UIColor clearColor];
    self.fieldPass.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.fieldUser.leftView.frame.size.width, self.fieldUser.leftView.frame.size.height)];
    NSDictionary *dicPass = @{NSForegroundColorAttributeName:[UIColor grayColor], NSFontAttributeName:[UIFont systemFontOfSize:20.0]};
    self.fieldPass.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" 请输入密码" attributes:dicPass];
    self.fieldPass.leftView.backgroundColor = [UIColor colorWithRed:0.12 green:0.09 blue:0.07 alpha:1.00];
    self.fieldPass.leftViewMode =UITextFieldViewModeAlways;
    //    fieldUser的UIBezierPath
    UIBezierPath *maskPathUser = [UIBezierPath bezierPathWithRoundedRect:self.fieldUser.leftView.bounds byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *maskLayerUser = [[CAShapeLayer alloc] init];
    maskLayerUser.frame = self.fieldUser.leftView.bounds;
    maskLayerUser.path = maskPathUser.CGPath;
    self.fieldUser.leftView.layer.mask = maskLayerUser;
    [self.userView addSubview:_fieldUser];
    //    fieldPass的UIBezierPath
    UIBezierPath *maskPathPass = [UIBezierPath bezierPathWithRoundedRect:self.fieldPass.leftView.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *maskLayerPass = [[CAShapeLayer alloc] init];
    maskLayerPass.frame = self.fieldPass.leftView.bounds;
    maskLayerPass.path = maskPathPass.CGPath;
    self.fieldPass.leftView.layer.mask = maskLayerPass;
    [self.userView addSubview:_fieldPass];
    self.fieldPass.delegate = self;
    self.imageUser = [UIImageView packImageViewFrame:CGRectMake(20, 35 / 2, 20, self.fieldUser.frame.size.height - 35) imagePhoto:[UIImage imageNamed:@"icon_username"] colorBack:nil action:nil target:self];
    self.imagePass = [UIImageView packImageViewFrame:CGRectMake(self.imageUser.frame.origin.x, 35 / 2, self.imageUser.frame.size.width, self.imageUser.frame.size.height) imagePhoto:[UIImage imageNamed:@"icon_password"] colorBack:nil action:nil target:self];
    [self.fieldUser.leftView addSubview:_imageUser];
    [self.fieldPass.leftView addSubview:_imagePass];
}
- (void)creatBtn {
    self.btnPass = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, WIDETH * 0.23, HEIGHT * 0.025)];
    _btnPass.center = CGPointMake(WIDETH * 0.85, HEIGHT * 0.64);
    [_btnPass setTitle:@"忘记密码" forState:UIControlStateNormal];
    _btnPass.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    _btnPass.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    _btnPass.backgroundColor = [UIColor clearColor];
    [_btnPass setImage:[UIImage imageNamed:@"wangjimima"] forState:UIControlStateNormal];
    [self.view addSubview:_btnPass];
    [_btnPass addTarget:self action:@selector(addChilkBtnPass:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)creatBtnRegister {
    self.btnRegister = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, WIDETH * 0.95, HEIGHT * 0.065)];
    _btnRegister.backgroundColor = [UIColor redColor];
    self.btnRegister.backgroundColor = [UIColor colorWithRed:0.00 green:0.71 blue:0.54 alpha:1.00];
    [self.btnRegister setTitle:@"登录" forState:UIControlStateNormal];
    _btnRegister.center = CGPointMake(WIDETH / 2, HEIGHT * 0.74);
    [self.view addSubview:_btnRegister];
    self.btnSign = [UIButton buttonWithType:UIButtonTypeCustom];
    _btnSign.frame = CGRectMake(0, 0, WIDETH  * 0.95, HEIGHT * 0.065);
    self.lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDETH * 0.95, HEIGHT * 0.065)];
    _lable.backgroundColor = [UIColor clearColor];
    _lable.text = @"注册拉勾";
    _lable.textAlignment = 1;
    _lable.textColor = [UIColor colorWithRed:0.00 green:0.73 blue:0.55 alpha:1.00];
    _lable.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnChilk:)];
    [_lable addGestureRecognizer:tap];
    _lable.layer.borderWidth = 0.4;
    _lable.layer.borderColor = [UIColor colorWithRed:0.00 green:0.73 blue:0.55 alpha:1.00].CGColor;
    _btnSign.backgroundColor = [UIColor colorWithRed:0.00 green:0.54 blue:0.40 alpha:1.00];
    _btnSign.alpha = 0.4;
    _btnSign.layer.borderWidth = 2;
    _btnSign.layer.borderColor = [UIColor colorWithRed:0.00 green:1 blue:1 alpha:1.00].CGColor;
    _btnSign.center = CGPointMake(WIDETH / 2, HEIGHT * 0.84);
    _lable.center = _btnSign.center;
    [self.view addSubview:_btnSign];
    [self.view addSubview:_lable];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField == _fieldUser) {
        if (_isPanduan) {
            self.imageUser.image = [UIImage imageNamed:@"user_login_logo_phone_selected"];
        } else {
            self.imageUser.image = [UIImage imageNamed:@"icon_username_select"];
        }
    } else {
        if (self.isPanduan) {
            self.imagePass.image = [UIImage imageNamed:@"user_login_logo_code_selected"];
        } else {
            self.imagePass.image = [UIImage imageNamed:@"icon_password_select"];
        }
    }
    return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if (textField == _fieldUser) {
        if (_isPanduan) {
            self.imageUser.image = [UIImage imageNamed:@"user_login_logo_phone"];
        } else {
            self.imageUser.image = [UIImage imageNamed:@"icon_username"];
        }
    } else {
        if (_isPanduan) {
            self.imagePass.image = [UIImage imageNamed:@"user_login_logo_code"];
        } else {
            self.imagePass.image = [UIImage imageNamed:@"icon_password"];
        }
    }
    return YES;
}

- (void)btnChilk:(UITapGestureRecognizer *)tap {
    NSDictionary *dicUs = @{NSForegroundColorAttributeName:[UIColor grayColor], NSFontAttributeName:[UIFont systemFontOfSize:20.0]};
    self.fieldUser.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" 请输入手机号" attributes:dicUs];
    NSDictionary *dicPa = @{NSForegroundColorAttributeName:[UIColor grayColor], NSFontAttributeName:[UIFont systemFontOfSize:20.0]};
    self.fieldPass.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" 请输入验证码" attributes:dicPa];
    self.fieldPass.rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDETH * 0.3, self.fieldPass.frame.size.height)];
    //    竖线
    UILabel *lableLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 2, self.fieldPass.frame.size.height * 0.45)];
    lableLine.backgroundColor = [UIColor whiteColor];
    lableLine.center = CGPointMake(self.fieldPass.rightView.frame.origin.x, self.fieldPass.frame.size.height / 2);
    //   验证码按钮
    self.btnSign = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.fieldPass.rightView.frame.size.width, self.fieldPass.rightView.frame.size.height)];
    _btnSign.backgroundColor = [UIColor clearColor];
    [_btnSign setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_btnSign setTitleColor:[UIColor colorWithRed:0.03 green:0.55 blue:0.44 alpha:1.00] forState:UIControlStateNormal];
    
    self.fieldPass.rightViewMode = UITextFieldViewModeAlways;
    
    [self.fieldPass.rightView addSubview:lableLine];
    self.fieldPass.rightView.backgroundColor = [UIColor clearColor];
    [_fieldPass.rightView addSubview:_btnSign];
    self.btnPass.hidden = YES;
    self.lable.text = @"返回登录";
    [self.btnRegister setTitle:@"注册" forState:UIControlStateNormal];
    self.imageUser.image = [UIImage imageNamed:@"user_login_logo_phone"];
    self.imagePass.image = [UIImage imageNamed:@"user_login_logo_code"];
    self.lableAgree = [UIButton buttonWithType:UIButtonTypeCustom];
    self.lableAgree.frame = CGRectMake(0, 0, WIDETH * 0.4, HEIGHT * 0.025);
    self.lableAgree.backgroundColor = [UIColor clearColor];
    self.lableAgree.center = CGPointMake(WIDETH * 0.32, HEIGHT * 0.64);
    [self.lableAgree setTitle:@"注册代表你已同意" forState:UIControlStateNormal];
    [self.lableAgree setImage:[UIImage imageNamed:@"xieyi "] forState:UIControlStateNormal];
    [self.view addSubview:_lableAgree];
    self.btnDelegate = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btnDelegate.frame = CGRectMake(0, 0, WIDETH * 0.3, self.lableAgree.frame.size.height);
    [self.btnDelegate setTitle:@"拉勾用户协议" forState:UIControlStateNormal];
    [self.btnDelegate setTitleColor:[UIColor colorWithRed:0.00 green:0.73 blue:0.55 alpha:1.00] forState:UIControlStateNormal];
    self.btnDelegate.center = CGPointMake(self.lableAgree.frame.size.width / 2 + self.lableAgree.center.x + self.lableAgree.frame.origin.x , self.lableAgree.center.y);
    self.btnDelegate.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_btnDelegate];
    
    [tap removeTarget:self action:@selector(btnChilk:)];
    [tap addTarget:self action:@selector(addChilk:)];
    self.isPanduan = YES;
}
- (void)addChilk:(UITapGestureRecognizer *)tap {
    NSDictionary *dicU = @{NSForegroundColorAttributeName:[UIColor grayColor], NSFontAttributeName:[UIFont systemFontOfSize:20.0]};
    self.fieldUser.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" 请输入已验证手机号或邮箱" attributes:dicU];
    NSDictionary *dicP = @{NSForegroundColorAttributeName:[UIColor grayColor], NSFontAttributeName:[UIFont systemFontOfSize:20.0]};
    self.fieldPass.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" 请输入密码" attributes:dicP];
    self.imageUser.image = [UIImage imageNamed:@"icon_username"];
    self.imagePass.image = [UIImage imageNamed:@"icon_password"];
    self.lable.text = @"注册拉勾";
    [_btnRegister setTitle:@"登录" forState:UIControlStateNormal];
    self.btnPass.hidden = NO;
    self.btnDelegate.hidden = YES;
    self.lableAgree.hidden = YES;
    self.fieldPass.rightView.hidden = YES;
    [tap removeTarget:self action:@selector(addChilk:)];
    [tap addTarget:self action:@selector(btnChilk:)];
    self.isPanduan = NO;
}
- (void)addChilkBtnPass:(UIButton *)btn {
    PassView *pass = [[PassView alloc] init];
    pass.modalTransitionStyle = 2;
    [self presentViewController:pass animated:YES completion:nil];
}
@end
